﻿package com.crayon.graphics.drawDemo
{
	import flash.display.Shape;
	import flash.display.GradientType;
	
	public class StarShape extends Shape
	{
		private var step:Number;
		private var halfStep:Number;
		private var start:Number;
		private var n:Number;
		private var dx:Number;
		private var dy:Number;
		
		public function StarShape(color:uint = 0xff0000, x:Number = 0, y:Number = 0, points:int = 5, innerRadius:Number = 20, outerRadius:Number = 50, angle:Number = 0)
		{
			
			var count:Number = Math.abs(points);
			this.graphics.lineStyle(2, 0x85DB18);
			
			//开始填色
			this.graphics.beginFill(color);
			if (count > 2)
			{
				// init vars
				step, halfStep, start, n, dx, dy;
				//计算两点之间距离
				step = (Math.PI * 2) / points;
				halfStep = step / 2;
				//起始角度
				start = (angle / 180) * Math.PI;
				this.graphics.moveTo(x + (Math.cos(start) * outerRadius), y - (Math.sin(start) * outerRadius));
				//画星状图的边
				for (n = 1; n <= count; n++)
				{
					dx = x + Math.cos(start + (step * n) - halfStep) * innerRadius;
					dy = y - Math.sin(start + (step * n) - halfStep) * innerRadius;
					this.graphics.lineTo(dx, dy);
					dx = x + Math.cos(start + (step * n)) * outerRadius;
					dy = y - Math.sin(start + (step * n)) * outerRadius;
					this.graphics.lineTo(dx, dy);
				}
			}
			this.graphics.endFill();
		}
	}
}